import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home/Home.vue'
import Layout from '../views/Layout'
import store from '../store/index'

Vue.use(VueRouter)

  const routes = [
  {
    path:'/',
    component:Layout,
    redirect:{name:'home'},
    children:[
      {
        path:'product',
        component:Home,
        name:'home',
        meta:{
          islogin:true
        }
      },
      {
        path:'params',
        component:()=>import('../views/Params/Params.vue'),
        name:'params',
        meta:{
          islogin:true
        }
      },
      {
        path:'content',
        component:()=>import('../views/Content/Content.vue'),
        name:'content',
        meta:{
          islogin:true
        }
      }
    ]
  },
  {
    path:'/login',
    component:()=>import('../views/Login/Login.vue'),
    name:'login'
  }
]

const router = new VueRouter({
  routes
})

// 前置守卫
router.beforeEach((to,from,next)=>{
  if(to.meta.islogin){
    // 需要从vuex中获取是否登录过
    var flag = store.state.token;
    if(flag){
      next();
    }else{
      next('/login')
    }
  }else{
    next()
  }
})

export default router
